<?php

class UcenterController extends BaseController{

    public function init(){
        parent::init();
        if(!$this->member['member_id']){
            if(IS_AJAX){
                $this->ajaxReturn(array('err'=>1,'msg'=>L('_no_login')));
            }else{
                $this->error(L('_no_login'));
            }
        }
    }

    //首页
    public function indexAction(){
        Yaf\loader::import(APP_PATH.'/common/lib/GoogleAuthenticator.php');//公共方法
        $ga = new PHPGangsta_GoogleAuthenticator();
        $secret = $ga->createSecret();
        $MemberModel = new MemberModel();
        $BankModel = new BankModel();
        $CurrencyModel = new CurrencyModel();
        $member = $MemberModel->getUserInfo($this->member['member_id']);
        $cardNum = $BankModel->where(array('member_id'=>$this->member['member_id']))->count();
        $currency=$CurrencyModel->getMarket();
        foreach ($currency as $k=>$v){
            //检查用户币种信息
            $rs=$CurrencyModel->getCurrencyUser($this->member['member_id'],$v['currency_id']);
            if(!$rs){
                $CurrencyModel->addCurrencyUser($this->member['member_id'],$v['currency_id']);
            }
        }
        $member['hidden_empty_currency'] = $MemberModel->get_field($this->member['member_id'],'hidden_empty_currency');
        $this->assign('member',$member);
        $this->assign('cardNum',$cardNum);
        $this->assign('currency',$CurrencyModel->currency());
        $this->assign('secret',$secret);
        $this->show('Ucenter/index.php');
    }

    //个人交易币
    public function get_currencyAction(){
        $pageSize = 3;
        $CurrencyModel = new CurrencyModel();
        $MemberModel = new MemberModel();
        $CurrencyUserModel = new Model('CurrencyUser');
        $hidden_empty_currency = $MemberModel->get_field($this->member['member_id'],'hidden_empty_currency');
        $where['member_id'] = $this->member['member_id'];
        if($hidden_empty_currency>0){
            $where['_string'] = ' (`num`<>0 or `forzen_num`<>0) ';
        }
        $count      = $CurrencyUserModel->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$pageSize,$this->input('param'));
        $Page->setConfig('theme','<div class="page no-margin pull-right">%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%</div>');
        $Page->setConfig('next','»');
        $Page->setConfig('prev','«');
        $Page->setConfig('header','');
        $show       = $Page->show();// 分页显示输出
        $list = $CurrencyUserModel->where($where)
                    ->field('cu_id,currency_id,num,forzen_num,in_url')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('cu_id desc')
                    ->select();
        foreach ($list as $key => $value) {
            $currency = $CurrencyModel->currency_one($value['currency_id'],'currency_name,currency_mark');
            $list[$key]['currency_name'] = $currency['currency_name'];
            $list[$key]['currency_mark'] = $currency['currency_mark'];
            $list[$key]['in_href'] = url('set_in_url',array('id'=>$value['cu_id']));
            $list[$key]['out_href'] = url('set_out_url',array('currency_id'=>$value['currency_id']));
            $list[$key]['withdraw_out_href'] = url('withdraw_out',array('currency_id'=>$value['currency_id']));
            $list[$key]['withdraw_out_check'] = url('withdraw_out_check',array('currency_id'=>$value['currency_id']));
        }
        $this->ajaxReturn(array('err'=>0,'list'=>$list,'page'=>$show));
    }

    //获取委托
    public function get_orderAction(){
        $pageSize = 10;
        $currency_id = $this->input('get','currency_id');
        if(isset($currency_id) && $currency_id!=''){
            $where['currency_id'] = intval($currency_id);
        }
        $trade_status = $this->input('get','status');
        if(isset($trade_status) && $trade_status=='completed'){
            $where['status'] = array('in',array(2,-1));
        }
        $page_size = $this->input('get','size');
        if(isset($page_size) && $page_size!=''){
            $pageSize = $page_size;
        }
        $OrdersModel = new OrdersModel();
        $CurrencyModel = new CurrencyModel();
        $where['member_id'] = $this->member['member_id'];
        $count      = $OrdersModel->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$pageSize,$this->input('param'));
        $Page->setConfig('theme','<div class="page no-margin pull-right">%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%</div>');
        $Page->setConfig('next','»');
        $Page->setConfig('prev','«');
        $Page->setConfig('header','');
        $show       = $Page->show();// 分页显示输出
        $list = $OrdersModel->where($where)
                    ->field('currency_id,currency_trade_id,price,num,trade_num,add_time,type,orders_id,status')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('add_time desc')
                    ->select();
        foreach ($list as $key => $value) {
            $currency = $CurrencyModel->getCurrencynameById($value['currency_id']);
            $trade_currency = $CurrencyModel->getCurrencynameById($value['currency_trade_id']);
            $list[$key]['currency'] = $currency['currency_mark'];
            $list[$key]['trade_currency'] = $trade_currency['currency_mark'];
            $list[$key]['addtime'] = date('Y-m-d H:i:s',$value['add_time']);
        }
        $this->ajaxReturn(array('err'=>0,'list'=>$list,'page'=>$show));
    }

    //获取交易记录
    public function get_tradeAction(){
        $pageSize = 10;
        $currency_id = $this->input('get','currency_id');
        if(isset($currency_id) && $currency_id!=''){
            $where['currency_id'] = intval($currency_id);
        }
        $TradeModel = new TradeModel();
        $CurrencyModel = new CurrencyModel();
        $where['member_id'] = $this->member['member_id'];
        $count      = $TradeModel->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$pageSize,$this->input('param'));
        $Page->setConfig('theme','<div class="page no-margin pull-right">%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%</div>');
        $Page->setConfig('next','»');
        $Page->setConfig('prev','«');
        $Page->setConfig('header','');
        $show       = $Page->show();// 分页显示输出
        $list = $TradeModel->where($where)
                    ->field('currency_id,currency_trade_id,price,num,money,add_time,type')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('add_time desc')
                    ->select();
        foreach ($list as $key => $value) {
            $currency = $CurrencyModel->getCurrencynameById($value['currency_id']);
            $trade_currency = $CurrencyModel->getCurrencynameById($value['currency_trade_id']);
            $list[$key]['currency'] = $currency['currency_mark'];
            $list[$key]['trade_currency'] = $trade_currency['currency_mark'];
            $list[$key]['addtime'] = date('Y-m-d H:i:s',$value['add_time']);
        }
        $this->ajaxReturn(array('err'=>0,'list'=>$list,'page'=>$show));
    }

    //充值记录
    public function get_payAction(){
        $pageSize = 6;
        $currency_id = $this->input('get','currency_id');
        if(isset($currency_id) && $currency_id!=''){
            $where['p.currency_id'] = intval($currency_id);
        }
        $PayModel = new PayModel();
        $where['p.member_id'] = $this->member['member_id'];
        $count      = $PayModel
                    ->where($where)
                    ->join('as p left join __CURRENCY__ as c on p.currency_id = c.currency_id')
                    ->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$pageSize,$this->input('param'));
        $Page->setConfig('theme','<div class="page no-margin pull-right">%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%</div>');
        $Page->setConfig('next','»');
        $Page->setConfig('prev','«');
        $Page->setConfig('header','');
        $show       = $Page->show();// 分页显示输出
        $list = $PayModel->where($where)
                    ->field('p.add_time,p.num,p.status,c.currency_mark')
                    ->join('as p left join __CURRENCY__ as c on p.currency_id = c.currency_id')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('p.add_time desc')
                    ->select();
        foreach ($list as $key => $value) {
            $list[$key]['addtime'] = date('Y-m-d H:i:s',$value['add_time']);
        }
        $this->ajaxReturn(array('err'=>0,'list'=>$list,'page'=>$show));
    }

    //提现记录
    public function get_withdrawAction(){
        $pageSize = 6;
        $currency_id = $this->input('get','currency_id');
        if(isset($currency_id) && $currency_id!=''){
            $where['w.currency_id'] = intval($currency_id);
        }
        $WithdrawModel = new WithdrawModel();
        $where['w.member_id'] = $this->member['member_id'];
        $count      = $WithdrawModel
                    ->where($where)
                    ->join('as w left join __CURRENCY__ as c on w.currency_id = c.currency_id')
                    ->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$pageSize,$this->input('param'));
        $Page->setConfig('theme','<div class="page no-margin pull-right">%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%</div>');
        $Page->setConfig('next','»');
        $Page->setConfig('prev','«');
        $Page->setConfig('header','');
        $show       = $Page->show();// 分页显示输出
        $list = $WithdrawModel->where($where)
                    ->field('w.withdraw_id,w.add_time,w.all_num,w.withdraw_fee,w.num,w.status,w.remarks,w.url,c.currency_mark')
                    ->join('as w left join __CURRENCY__ as c on w.currency_id = c.currency_id')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('w.add_time desc')
                    ->select();
        foreach ($list as $key => $value) {
            $list[$key]['addtime'] = date('Y-m-d H:i:s',$value['add_time']);
        }
        $this->ajaxReturn(array('err'=>0,'list'=>$list,'page'=>$show));
    }

    //添加银行卡
    public function add_cardAction(){
        if(IS_POST){
            $BankModel = new Model('Bank');
            $post = $this->input('post','info');
            $validate = array(
                array('bank_name','require','bank_name>'.L('_bank_required')),
                array('card_num','require','card_num>'.L('_card_num_required')),
                array('card_num',"/^([1-9]{1})(\d{14}|\d{18})$/",'card_num>'.L('_card_num_err')),
                array('card_name','require','card_name>'.L('_card_name_required')),
            );
            $check_result = $BankModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$BankModel->getError());
                $this->ajaxReturn($json);
            }else{
                $post['add_time'] = time();
                $post['member_id'] = $this->member['member_id'];
                $result = $BankModel->add($post);
                if($result){
                    MemberModel::log($this->member['member_id'],$post,1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success')));
                }else{
                    MemberModel::log($this->member['member_id'],$post,0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
                }
            }
        }
    }

    //绑定google认证
    public function authAction(){
        Yaf\loader::import(APP_PATH.'/common/lib/GoogleAuthenticator.php');//公共方法
        $ga = new PHPGangsta_GoogleAuthenticator();
        if(IS_POST){
            $MemberModel = new MemberModel();
            $sk = $this->input('post','sk');
            $code = $this->input('post','code');
            $checkResult = $ga->verifyCode($sk, $code, 2);
            if ($checkResult) {
                $data = array(
                    'auth'=>$sk,
                    'auth_level'=>3
                );
                $res = $MemberModel->modify($this->member['member_id'],$data);
                if($res){
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success')));
                }else{
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
                }
            } else {
                $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
            } 
        }
    }


    //充值
    public function inAction(){
        $this->show('Ucenter/in.php');
    }

    //提现
    public function outAction(){
        $BankModel = new Model('Bank');
        $SettingModel = new SettingModel();
        $MemberModel = new MemberModel();
        $WithdrawModel = new WithdrawModel();
        //提现手续费
        $fee = $this->setting['withdraw_fee']?$this->setting['withdraw_fee']:0.1;
        //提现最小总量
        $min_withdraw = $this->setting['min_withdraw_num']?$this->setting['min_withdraw_num']:100;
        //账户余额
        $account = $MemberModel->getUserInfo($this->member['member_id']);
        //绑定的银行卡
        $bank = $BankModel->where(array('member_id'=>$this->member['member_id']))->select();
        if(IS_POST){
            $post = $this->input('post','info');
            $validate = array(
                array('bank_id','require','bank_id>'.L('_card_num_required')),
                array('withdraw_num','require','withdraw_num>'.L('_withdraw_num_required')),
            );
            $check_result = $WithdrawModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$WithdrawModel->getError());
                $this->ajaxReturn($json);
            }else{
                if($post['withdraw_num']<$min_withdraw){
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_withdraw_num_gt').$min_withdraw));
                }
                if($post['withdraw_num']>$this->setting['max_withdraw_num']){
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_withdraw_num_lt').$this->setting['max_withdraw_num']));
                }
                //google或者邮件验证
                if(isset($post['auth_level']) && $post['auth_level']==$this->member['auth_level']){
                    $res = $this->auth('withdraw_rmb',$post);
                    if($res>0) $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
                }else{
                    if($this->member['auth_level']==1 || $this->member['auth_level']==3){
                        $res = $this->send_email($this->member['email'],'withdraw_rmb',L('withdraw_email_title'),'withdraw_html');
                        if($res['err']==0){
                            $this->ajaxReturn(array('err'=>2,'msg'=>'auth','auth_level'=>$this->member['auth_level']));
                        }else{
                            $this->ajaxReturn(array('err'=>1,'msg'=>$res['msg'],'auth_level'=>$this->member['auth_level']));
                        }
                    }
                    $this->ajaxReturn(array('err'=>2,'msg'=>'auth','auth_level'=>$this->member['auth_level']));
                }
                $result = $WithdrawModel->out($this->member['member_id'],$post['withdraw_num'],$fee,$post['bank_id'],0,1);
                if($result){
                    MemberModel::log($this->member['member_id'],$post,1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success_wait'),'url'=>url('ucenter/index')));
                }else{
                    MemberModel::log($this->member['member_id'],$post,0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
                }
            }
        }else{
            if(!$bank){
                $this->error(L('_no_bind_bank_card'),url('ucenter/index'));
            }
            $this->assign('bank',$bank);
            $this->assign('fee',$fee);
            $this->assign('min_withdraw_num',$min_withdraw);
            $this->assign('account',$account);
            $this->show('Ucenter/out.php');
        }
    }

    //设置
    public function settingAction(){
        $MemberModel = new MemberModel();
        $user = $MemberModel->field('nick,name,phone,birthday,country,province,city,area,address')
                ->where(array('member_id'=>$this->member['member_id']))
                ->find();
        $this->assign('user',$user);
        $this->show('Ucenter/setting.php');
    }

    /**
     * [get_auth_qrcodeAction google验证二维码]
     * @return [type] [description]
     */
    public function get_auth_qrcodeAction(){
        Yaf\loader::import(APP_PATH.'/common/lib/phpqrcode.php');
        $name = urlencode($this->setting['web_title'].'（'.$this->member['email'].'）');
        $sk = $this->input('get','sk');
        $str = 'otpauth://totp/'.$name.'?secret='.$sk.'';
        QRcode::png($str,false,0,10,2);
    }

    /**
     * [set_out_urlAction 设置提币地址]
     */
    public function set_out_urlAction(){
        if(IS_POST){
            $WithdrawAddressModel = new Model('WithdrawAddress');
            $CurrencyUserModel = new Model('CurrencyUser');
            $currency_id = $this->input('get','currency_id');
            $post= $this->input('post','info');
            $post['currency_id'] = $currency_id;
            $validate = array(
                array('currency_id','require','currency_id>币种id不能为空'),
                array('url','require','url>提币钱包地址不能为空'),
            );
            $check_result = $WithdrawAddressModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$CurrencyUserModel->getError());
                $this->ajaxReturn($json);
            }else{
                $post['member_id'] = $this->member['member_id'];
                $post['add_time'] = time();
                //google或者邮件验证
                if(isset($post['auth_level']) && $post['auth_level']==$this->member['auth_level']){
                    $res = $this->auth('withdraw_address',$post);
                    if($res>0) $this->ajaxReturn(array('err'=>1,'msg'=>'验证失败，请重试'));
                }else{
                    if($this->member['auth_level']==1 || $this->member['auth_level']==3){
                        $res = $this->send_email($this->member['email'],'withdraw_address',L('withdraw_address_email_title'),'withdraw_address_html');
                        if($res['err']==0){
                            $this->ajaxReturn(array('err'=>2,'msg'=>'auth','auth_level'=>$this->member['auth_level']));
                        }else{
                            $this->ajaxReturn(array('err'=>1,'msg'=>$res['msg'],'auth_level'=>$this->member['auth_level']));
                        }
                    }
                    $this->ajaxReturn(array('err'=>2,'msg'=>'auth','auth_level'=>$this->member['auth_level']));
                }
                $result = $WithdrawAddressModel->add($post);
                if($result){
                    MemberModel::log($this->member['member_id'],$post,1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success')));
                }else{
                    MemberModel::log($this->member['member_id'],$post,0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('server_busy')));
                }
            }
        }
    }
    
    /**
     * [set_in_urlAction 设置转入钱包地址]
     */
    public function set_in_urlAction(){
        if(IS_POST){
            $CurrencyModel = new CurrencyModel();
            $cu_id = $this->input('get','id');
            $in_url = $CurrencyModel->get_in_url($this->member['member_id'],$cu_id);
            if($in_url){
                $this->ajaxReturn(array('err'=>0,'msg'=>L('_pay_address_success'),'in_url'=>$in_url));
            }else{
                $result = $CurrencyModel->set_in_url($this->member['member_id'],$cu_id);
                if($result){
                    MemberModel::log($this->member['member_id'],'设置转入的钱包地址',1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_pay_address_success'),'in_url'=>$result));
                }else{
                    MemberModel::log($this->member['member_id'],'设置转入的钱包地址失败',0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_pay_address_err')));
                }
            }
            
        }
    }

    /**
     * [withdraw_out_checkAction 检测币是否可以转出]
     * @return [type] [description]
     */
    public function withdraw_out_checkAction(){
        if(IS_POST){
            $CurrencyModel = new CurrencyModel();
            $WithdrawAddressModel = new Model('WithdrawAddress');
            $currency_id = $this->input('get','currency_id');
            $address = $WithdrawAddressModel
                        ->field('url,wa_id,currency_id,remarks')
                        ->where(array('member_id'=>$this->member['member_id'],'currency_id'=>$currency_id))
                        ->select();
            if($address){
                $currency = $CurrencyModel->getCurrencyUser($this->member['member_id'],$currency_id);
                $currency['currency'] = $CurrencyModel->getCurrencynameById($currency['currency_id']);
                $this->ajaxReturn(array('err'=>0,'msg'=>'ok','address'=>$address,'currency'=>$currency));
            }else{
                $this->ajaxReturn(array('err'=>1,'msg'=>L('_widthdraw_no_address')));
            }
        }
    }

    /**
     * [withdraw_outAction 币种转出]
     * @return [type] [description]
     */
    public function withdraw_outAction(){
        if(IS_POST){
            $CurrencyModel = new CurrencyModel();
            $WithdrawModel = new WithdrawModel();
            $WithdrawAddressModel = new Model('WithdrawAddress');
            $post = $this->input('post','info');
            $post['currency_id'] = $this->input('get','currency_id');
            $validate = array(
                array('currency_id','require','currency_id>'.L('_currency_required')),
                array('withdraw_num','require','withdraw_num>'.L('_withdraw_num_required')),
                array('url','require','url>'.L('_withdraw_address_required')),
            );
            $check_result = $CurrencyModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$CurrencyModel->getError());
                $this->ajaxReturn($json);
            }else{
                $addressNum = $WithdrawAddressModel
                            ->where(array('member_id'=>$this->member['member_id'],'currency_id'=>$post['currency_id'],'url'=>$post['url']))
                            ->count();
                if($addressNum<=0){
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_widthdraw_no_address')));
                }
                $currency = $CurrencyModel->getCurrencynameById($post['currency_id']);
                if($post['withdraw_num']<$currency['currency_withdraw_min']){
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_withdraw_num_gt').$currency['currency_withdraw_min']));
                }
                if($post['withdraw_num']>$currency['currency_withdraw_max']){
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_withdraw_num_lt').$currency['currency_withdraw_max']));
                }
                $num = $CurrencyModel->getUserMoney($this->member['member_id'],$post['currency_id'],'num');
                if($post['withdraw_num']>$num){
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_amount_enough')));
                }
                //google或者邮件验证
                if(isset($post['auth_level']) && $post['auth_level']==$this->member['auth_level']){
                    $res = $this->auth('withdraw_coin',$post);
                    if($res>0) $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
                }else{
                    if($this->member['auth_level']==1 || $this->member['auth_level']==3){
                        $res = $this->send_email($this->member['email'],'withdraw_coin',L('withdraw_email_title'),'withdraw_html');
                        if($res['err']==0){
                            $this->ajaxReturn(array(
                                'err'=>2,
                                'msg'=>'auth',
                                'auth_level'=>$this->member['auth_level'],
                                'currency_withdraw_min'=>$currency['currency_withdraw_min'],
                                'currency_withdraw_max'=>$currency['currency_withdraw_max']
                            ));
                        }else{
                            $this->ajaxReturn(array(
                                'err'=>1,
                                'msg'=>$res['msg'],
                                'auth_level'=>$this->member['auth_level'],
                                'currency_withdraw_min'=>$currency['currency_withdraw_min'],
                                'currency_withdraw_max'=>$currency['currency_withdraw_max']
                            ));
                        }
                    }
                    $this->ajaxReturn(array(
                        'err'=>2,
                        'msg'=>'auth',
                        'auth_level'=>$this->member['auth_level'],
                        'currency_withdraw_min'=>$currency['currency_withdraw_min'],
                        'currency_withdraw_max'=>$currency['currency_withdraw_max']
                    ));
                }
                $result = $WithdrawModel->out($this->member['member_id'],$post['withdraw_num'],$currency['currency_withdraw_fee'],'',$post['currency_id'],1,$post['url']);
                if($result){
                    MemberModel::log($this->member['member_id'],$post,1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success_wait'),'url'=>url('ucenter/index')));
                }else{
                    MemberModel::log($this->member['member_id'],$post,0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('server_busy')));
                }
            }
        }
    }

    public function cancel_withdrawAction(){
        $withdraw_id = $this->input('get','withdraw_id');
        $WithdrawModel = new WithdrawModel();
        $count = $WithdrawModel->where(array('member_id'=>$this->member['member_id'],'withdraw_id'=>$withdraw_id))->count();
        if($count<=0){
            $this->ajaxReturn(array('err'=>1,'msg'=>L('_cancel_err')));
        }
        $result = $WithdrawModel->setStatus($withdraw_id,5,0,'user_cancel');
        if($result){
            $this->ajaxReturn(array('err'=>0,'msg'=>L('_cancel_success')));
        }else{
            $this->ajaxReturn(array('err'=>1,'msg'=>L('server_busy')));
        }
    }

    /**
     * [set_show_empty_currencyAction 设置是否空币种显示]
     */
    public function set_show_empty_currencyAction(){
        if(IS_POST){
            $MemberModel = new MemberModel();
            $show = $this->input('get','show');
            $post['member_id'] = $this->member['member_id'];
            $post['hidden_empty_currency'] = $show=='on'?1:0;
            $result = $MemberModel->modify($post['member_id'],$post);
            if($result){
                MemberModel::log($this->member['member_id'],$post,1);
                $this->ajaxReturn(array('err'=>0,'msg'=>L('_success')));
            }else{
                MemberModel::log($this->member['member_id'],$post,0);
                $this->ajaxReturn(array('err'=>1,'msg'=>L('server_busy')));
            }
        }
    }

    /**
     * [personalAction 个人信息]
     * @return [type] [description]
     */
    public function personalAction(){
        if(IS_POST){
            $MemberModel = new MemberModel();
            $post = $this->input('post','info');
            $validate = array(
                array('nick','require','nick>'.L('_nickname_required')),
                array('name','require','name>'.L('_name_required')),
                array('birthday','Validate::isBirthDate','birthday>'.L('_birthday_err'),1,'function'),
                array('address','require','address>'.L('_address_required')),
                array('phone','Validate::isMobilePhone','phone>'.L('_phone_required'),1,'function'),
            );
            $check_result = $MemberModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$MemberModel->getError());
                $this->ajaxReturn($json);
            }else{
                $post['member_id'] = $this->member['member_id'];
                $result = $MemberModel->modify($post['member_id'],$post);
                if($result){
                    MemberModel::log($this->member['member_id'],$post,1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success')));
                }else{
                    MemberModel::log($this->member['member_id'],$post,0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('server_busy')));
                }
            }
        }
    }

    /**
     * [send_repwd_emailAction 发送修改密码短信]
     * @return [type] [description]
     */
    public function send_repwd_emailAction(){
        if(IS_POST){
            $res = $this->send_email($this->member['email'],'repwd',L('re_pwd_email_title'),'re_pwd_html');
            if($res['err']==0){
                $this->ajaxReturn(array('err'=>0,'msg'=>L('_send_success')));
            }else{
                $this->ajaxReturn(array('err'=>1,'msg'=>$res['msg']));
            }
        }
    }
    /**
     * [change_pwdAction 修改密码]
     * @return [type] [description]
     */
    public function change_pwdAction(){
        if(IS_POST){
            $MemberModel = new MemberModel();
            $Rsa = new Rsa();
            $code = Cookie::getInstance()->get(md5('repwd'));
            $decode = $Rsa->privDecrypt($code);
            $decode = json_decode($decode,true);
            $post = $this->input('post','info');
            $post['checkcode'] =  $decode['code'];
            if(time()-$decode['time']>180){
                $this->ajaxReturn(array('err'=>0,'msg'=>L('_code_timeout')));
            }
            $validate = array(
                array('oldpwd','require','oldpwd>'.L('_old_pwd_required')),
                array('pwd','require','pwd>'.L('_pwd_required')),
                array('repwd','pwd','repwd>'.L('_pwd_repwd_diff'),0,'confirm'),
                array('code','require','code>'.L('_code_required')),
                array('checkcode','code','code>'.L('_code_err'),0,'confirm')
            );
            $check_result = $MemberModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$MemberModel->getError());
                $this->ajaxReturn($json);
            }else{
                $pwd = $MemberModel->where(array('member_id'=>$this->member['member_id']))->getField('pwd');
                if($pwd!==encrypt($post['oldpwd'])){
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_pwd_err')));
                }
                $data['member_id'] = $this->member['member_id'];
                $data['pwd'] = encrypt($post['pwd']);
                Cookie::getInstance()->del(md5('repwd'));
                $result = $MemberModel->modify($data['member_id'],$data);
                if($result){
                    MemberModel::log($this->member['member_id'],'修改密码成功',1);
                    $MemberModel->loginOut();
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success'),'action'=>'redirect','url'=>url('member/login')));
                }else{
                    MemberModel::log($this->member['member_id'],'修改密码失败',0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('server_busy')));
                }
            }
        }
    }
    
}